﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DataShow
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            UIInit();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void UIInit()
        {
            //得到前三名预报员（前台完成）

            //得到旬月数据
            FillXun();
            
            //得到常用文件下载
            FillFiles();

            //加载服务重点
            FillZhongDian();
        }

        #region 服务重点
        /// <summary>
        /// 服务重点
        /// </summary>
        private void FillZhongDian()
        {
            string sql = "select top 1 * from msg";
            lbl_fwzd.Text = new codeDAL.SqlHelper().ExecuteScalar(sql);

        } 
        #endregion


        /// <summary>
        /// 获得所有文件链接
        /// </summary>
        private void FillFiles()
        {
            string sql = "select * from files";
            string result = "";
            DataTable dt = new codeDAL.SqlHelper().ExecuteQuery(sql, CommandType.Text);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    result += "<h4><a target='_black' href='" + Path.Combine("uploadfiles", dt.Rows[i]["downloadpath"].ToString()) + "'>" + dt.Rows[i]["filename"].ToString() + "</a></h4>";
                }
                lbl_files.Text = result;
            }
        }

        #region 填充旬月数据
        /// <summary>
        /// 填充旬月数据
        /// </summary>
        private void FillXun()
        {
            DateTime time = DateTime.Now;
            string month = time.Month.ToString();
            string xun = "";
            int day = time.Day;
            if (day > 0 && day <= 10) //上旬
            {
                xun = "01";
            }
            else if (day > 10 && day <= 20)
            {
                xun = "02";
            }
            else
            {
                xun = "03";
            }
            string sql = string.Format("select content from xunyue where [month]='{0}' and xun='{1}'",month.Length==2?month:("0"+month),xun);
            lbl_xun.Text = new codeDAL.SqlHelper().ExecuteScalar(sql).ToString();
            sql = string.Format("select content from xunyue where [month]='{0}' and xun='00'", month.Length == 2 ? month : ("0" + month));
            lbl_yue.Text = new codeDAL.SqlHelper().ExecuteScalar(sql).ToString();
        }
        
        #endregion

        #region 用户登录按钮
        /// <summary>
        /// 用户登录按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lb_login_Click(object sender, EventArgs e)
        {
            string username = txt_username.Text.Trim();
            string password = txt_password.Text.Trim();
            if (new codeBLL.UserBLL().Login(username, password))
            {
                Session.Add("nowuser", username);
                if (CheckIsForecaster(username))
                {
                    Session.Add("isadmin",false);
                    Response.Redirect("/ForecasterIndex.aspx");
                }
                else
                {
                    Session.Add("isadmin", true);
                    Response.Redirect("/index.aspx");
                }
                
                Response.End();
            }
            else
            {
                string ErrorMSg = "<div class='alert alert-error'><i class='icon-remove-sign'></i>用户名没有激活或用户名密码不匹配</div>";
                lbl_errormsg.Text = ErrorMSg;
                Common.RegisterScriptAtPageLast(Page,"CallModal()");
            }
        }
        private bool CheckIsForecaster(string username)
        {
            string sql = "select is_forcaster from account_forecaster where username='" + username+"'";
            codeDAL.SqlHelper helper = new codeDAL.SqlHelper();
            return helper.ExecuteScalar(sql).ToString()=="True";
        }
        #endregion

        protected void lbl_edit_Click(object sender, EventArgs e)
        {
            Response.Redirect("/ZDEdit.aspx");
            Response.End();
        }
    }
}